<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="1000px" scroll>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <!-- <el-form-item label="关联藏品ID" prop="collectionId">
        <el-input v-model="formData.collectionId" placeholder="请输入关联藏品ID" />
      </el-form-item> -->
      <el-form-item label="成果名称" prop="resultName">
        <el-input v-model="formData.resultName" placeholder="请输入成果名称" />
      </el-form-item>
      <el-form-item label="文件格式" prop="fileFormat">
        <el-input v-model="formData.fileFormat" placeholder="请输入文件格式" />
      </el-form-item>
      <el-form-item label="加工日期" prop="processingDate">
        <el-date-picker
          v-model="formData.processingDate"
          type="date"
          value-format="x"
          placeholder="选择加工日期"
        />
      </el-form-item>
      <el-form-item label="加工单位" prop="processingUnit">
        <el-input v-model="formData.processingUnit" placeholder="请输入加工单位" />
      </el-form-item>
      <el-form-item label="加工人" prop="processor">
        <el-input v-model="formData.processor" placeholder="请输入加工人" />
      </el-form-item>
      <el-form-item label="加工成果质量检查人" prop="qualityInspector">
        <el-input v-model="formData.qualityInspector" placeholder="请输入加工成果质量检查人" />
      </el-form-item>
      <el-form-item label="文件大小(M)" prop="fileSize">
        <el-input v-model="formData.fileSize" placeholder="请输入文件大小(M)" />
      </el-form-item>
      <el-form-item label="模型精度(mm)" prop="modelAccuracy">
        <el-input v-model="formData.modelAccuracy" placeholder="请输入模型精度(mm)" />
      </el-form-item>
      <el-form-item label="平均点间距(mm)" prop="avgPointSpacing">
        <el-input v-model="formData.avgPointSpacing" placeholder="请输入平均点间距(mm)" />
      </el-form-item>
      <el-form-item label="最大点间距(mm)" prop="maxPointSpacing">
        <el-input v-model="formData.maxPointSpacing" placeholder="请输入最大点间距(mm)" />
      </el-form-item>
      <el-form-item label="贴图分辨率(dpi)" prop="textureResolution">
        <el-input v-model="formData.textureResolution" placeholder="请输入贴图分辨率(dpi)" />
      </el-form-item>
      <el-form-item label="采集单位" prop="acquisitionUnit">
        <el-input v-model="formData.acquisitionUnit" placeholder="请输入采集单位" />
      </el-form-item>
      <el-form-item label="采集地点" prop="acquisitionLocation">
        <el-input v-model="formData.acquisitionLocation" placeholder="请输入采集地点" />
      </el-form-item>
      <el-form-item label="采集日期" prop="acquisitionDate">
        <el-date-picker
          v-model="formData.acquisitionDate"
          type="date"
          value-format="x"
          placeholder="选择采集日期"
        />
      </el-form-item>
      <el-form-item label="采集人员" prop="acquisitionPersonnel">
        <el-input v-model="formData.acquisitionPersonnel" placeholder="请输入采集人员" />
      </el-form-item>
      <el-form-item label="采集设备品牌型号" prop="equipmentModel">
        <el-input v-model="formData.equipmentModel" placeholder="请输入采集设备品牌型号" />
      </el-form-item>
      <el-form-item label="采集影像总数(张)" prop="totalImages">
        <el-input v-model="formData.totalImages" placeholder="请输入采集影像总数(张)" />
      </el-form-item>
      <el-form-item label="采集质量检查人" prop="acquisitionInspector">
        <el-input v-model="formData.acquisitionInspector" placeholder="请输入采集质量检查人" />
      </el-form-item>
      <el-form-item label="分辨率(dpi)" prop="resolution">
        <el-input v-model="formData.resolution" placeholder="请输入分辨率(dpi)" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { Collection3dAcquisitionApi, Collection3dAcquisitionVO } from '@/api/museum/collection3dacquisition'

/** 藏品三维采集信息 表单 */
defineOptions({ name: 'Collection3dAcquisitionForm' })
const props = defineProps({
  data: {
    type: Object,
    default: () => ({})
  }
})

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  collectionId: undefined,
  resultName: undefined,
  fileFormat: undefined,
  processingDate: undefined,
  processingUnit: undefined,
  processor: undefined,
  qualityInspector: undefined,
  fileSize: undefined,
  modelAccuracy: undefined,
  avgPointSpacing: undefined,
  maxPointSpacing: undefined,
  textureResolution: undefined,
  acquisitionUnit: undefined,
  acquisitionLocation: undefined,
  acquisitionDate: undefined,
  acquisitionPersonnel: undefined,
  equipmentModel: undefined,
  totalImages: undefined,
  acquisitionInspector: undefined,
  resolution: undefined
})
const formRules = reactive({
  collectionId: [{ required: true, message: '关联藏品ID不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await Collection3dAcquisitionApi.getCollection3dAcquisition(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as Collection3dAcquisitionVO
    if (formType.value === 'create') {
      await Collection3dAcquisitionApi.createCollection3dAcquisition({
        ...data,
        collectionId: props.data.id
      })
      message.success(t('common.createSuccess'))
    } else {
      await Collection3dAcquisitionApi.updateCollection3dAcquisition(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    collectionId: undefined,
    resultName: undefined,
    fileFormat: undefined,
    processingDate: undefined,
    processingUnit: undefined,
    processor: undefined,
    qualityInspector: undefined,
    fileSize: undefined,
    modelAccuracy: undefined,
    avgPointSpacing: undefined,
    maxPointSpacing: undefined,
    textureResolution: undefined,
    acquisitionUnit: undefined,
    acquisitionLocation: undefined,
    acquisitionDate: undefined,
    acquisitionPersonnel: undefined,
    equipmentModel: undefined,
    totalImages: undefined,
    acquisitionInspector: undefined,
    resolution: undefined
  }
  formRef.value?.resetFields()
}
</script>
